package com.vcredit.creditcat.manager.emall;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.creditcat.api.IAlipayProcessManager;
import com.vcredit.creditcat.dto.emallcredit.AlipayProcessInDto;
import com.vcredit.creditcat.enumtype.AlipayQRCodeStatusEnum;
import com.vcredit.creditcat.enumtype.BirdResponeEnum;
import com.vcredit.creditcat.enumtype.EMStatusEnum;
import com.vcredit.creditcat.exception.DataException;
import com.vcredit.creditcat.model.AuthAlipay;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.service.AuthAlipayService;
import com.vcredit.bird.service.base.BirdResponse;
import com.vcredit.bird.service.emall.AlipayProcessService;
import com.vcredit.creditcat.util.Constants;
import com.vcredit.framework.annotation.Log;

@Service(protocol = { "protocol1" }, version = "1.0")
public class AlipayProcessManager implements IAlipayProcessManager {

	@Autowired
	private AuthAlipayService authAlipayService;
	@Autowired
	private AlipayProcessService alipayProcessService;
	
	@Override
	@Log
	public void alipayCookieCrawl(AlipayProcessInDto param, Cust cust) throws Exception {
		BirdResponse initResult = alipayProcessService.alipayCookieInit();
		param.setToken(initResult.getToken());
		BirdResponse loginResult = alipayProcessService.alipayCookieLogin(param.getUserName(), param.getPassword(),
				param.getUa(), param.getRdsFormToken(), param.getCookies(), param.getToken(), Constants.ALIPAY_BUSTYPE);
		EMStatusEnum alipayState = Integer.parseInt(loginResult.getNextCode()) == BirdResponeEnum.Over.getCode()
				? EMStatusEnum.CRAWING : EMStatusEnum.INIT;
		AuthAlipay authAlipay = new AuthAlipay();
		authAlipay.setProcessId(cust.getLastProccessId());
		authAlipay.setAuthStatus(alipayState.getCode());
		authAlipay.setToken(param.getToken());
		authAlipay.setExpiredTime("");
		authAlipay.setCust(cust);
		authAlipayService.updateCreditAlipay(authAlipay);
	}

	@Override
	public List<AuthAlipay> crawlAlipay() {
		List<AuthAlipay> unCrawl = authAlipayService.findUnCrawl();
		return unCrawl;
	}

	@Override
	public void updateCrawlState(AuthAlipay param) throws Exception {
		authAlipayService.updateCreditAlipay(param);		
	}

	@Override
	public String getQRCode(AlipayProcessInDto param,Cust cust) throws Exception {
		
		BirdResponse responseInit = alipayProcessService.getORCode(Constants.ALIPAY_LOGIN_QCODE, Constants.ALIPAY_BUSTYPE);
		AuthAlipay authAlipay = new AuthAlipay();
		authAlipay.setProcessId(cust.getLastProccessId());
		authAlipay.setAuthStatus(EMStatusEnum.INIT.getCode());
		authAlipay.setToken(responseInit.getToken());
		authAlipay.setExpiredTime("");
		authAlipay.setCust(cust);
		authAlipayService.updateCreditAlipay(authAlipay);
		return responseInit.getVerCodeBase64();
	}

	@Override
	public String checkQRCode(Cust cust) throws Exception {
		AuthAlipay authAlipay= authAlipayService.findByProcessId(cust.getLastProccessId());
		if(null == authAlipay){
			throw new DataException("请先获取支付宝二维码校验"); 
		}
		BirdResponse initResult = alipayProcessService.checkORCode(authAlipay.getToken(),Constants.ALIPAY_BUSTYPE);
		if(StringUtils.isBlank(initResult.getResult())){
			return AlipayQRCodeStatusEnum.CHECKEFAIL.getCode().toString();
		}
		Integer status = Integer.parseInt(initResult.getResult());
		if(AlipayQRCodeStatusEnum.SUCCESS.getCode().equals(status)){			
			AuthAlipay newAuthAlipay = new AuthAlipay();
			newAuthAlipay.setProcessId(cust.getLastProccessId());
			newAuthAlipay.setAuthStatus(EMStatusEnum.CRAWING.getCode());
			authAlipayService.updateCreditAlipay(newAuthAlipay);
		}
		return initResult.getResult();
	}
}
